< previous page page_134 next page >

Page 134
the collection, copy the object reference to a defined object variable first. Using an object reference while it's in the collection is slower than using it after placing it in an object variable. The following is an example of this concept:
Dim txtInputCurrent As TextBox
    txtInputCurrent = ColTextInputLines.Item(1)
    txtInputCurrent.ToolTipText = Please enter your First name.
    txtInputCurrent.FontBold = True
In many cases, if a collection object can be used, an advanced Visual Basic programmer will never use an array. Instead of using an array that must be declared to a certain size and can contain only one data type, the collection can have items added and removed from it without having to worry about the storage that it needs. If you need the user to input an unknown number of names, phone numbers, or any other type of data, you could use an array as shown here:
Dim Input_List() as String
Dim Current_Value as String
Dim Counter as Long
    Counter = 0
Get_Next_Number:
    Current_Value = Inputbox(Enter the Phone Number,, NA)
If Current_Value <> NA then
        Counter = Counter + 1
        ReDim Input_List(Counter)
        Input_List(Counter) = Current_Value
    End If
    If Current_Value = END Then Exit Sub
    Goto Get_Next_Number
As you can see, when using arrays you must keep track of the index value, and you must resize it every time you need to add a new element to it. With a collection, the same routine would resemble the following code:
Dim Input_Col as New Collection
Dim Current_Value as String
Get_Next_Number:
    Current_Value = Inputbox(Enter the Phone Number,, NA)
    If Current_Value <> NA then
        Input_List.Add = Current_Value
    End If
    If Current_Value = END Then Exit Sub
    Goto Get_Next_Number
By using the collection, the process has become simpler and easier to manage.
Control Arrays
One of the things that you can do with objects is to use them as arrays. For example, you can define an array of text boxes on a form. Because of the way objects get created, you

 
< previous page page_134 next page >

If you like this book, buy it!